package com.songcha.module_home.ui.activity.search

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.songcha.library_base.interfaces.IApiResult
import com.songcha.library_base.interfaces.IResult
import com.songcha.library_base.mvvm.base.BaseViewModel
import com.songcha.library_business.bean.book.BookNetBean
import com.songcha.library_business.proxy.database.BookSearchHistoryDaoManager
import com.songcha.library_database_douyue.bean.BookSearchHistoryBean
import com.songcha.library_network.bean.BaseBean
import com.songcha.module_home.bean.SearchListDataBean

class SearchViewModel(app: Application):BaseViewModel<SearchRepository>(app) {

    val searchHistoryList=MutableLiveData<MutableList<BookSearchHistoryBean>>()
    val searchList=MutableLiveData<MutableList<BookNetBean>>()

    fun getSearchHistory(){
        handleDataObserver(repository!!.getSearchHistory(),object : IResult<MutableList<BookSearchHistoryBean>>{
            override fun onSuccess(data: MutableList<BookSearchHistoryBean>) {
                searchHistoryList.value=data
            }

        })
        //repository!!.getSearchHistory()
    }

    fun searchBook(keyword:String){
        BookSearchHistoryDaoManager.addSearchHistory(keyword)
        handleApiDataObserver(repository!!.searchBook(keyword),object : IApiResult<SearchListDataBean> {
            override fun onSuccess(data: SearchListDataBean) {
                searchList.value=data.data
            }

            override fun onError(e: Throwable) {

            }

        },true)
    }
}